package com.cykj.service.imp;

import com.cykj.bean.MyGameRoom;
import com.cykj.bean.Tblgame;
import com.cykj.bean.Tblroomuser;
import com.cykj.mapper.GameHallMapper;
import com.cykj.service.GameHallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GameHallServiceImp implements GameHallService {
    @Autowired
    private GameHallMapper gameHallMapper;

    /**
     * 找到游戏类型
     *
     * @param paramameterid paramameterid
     * @return {@link List }<{@link Tblgame }>
     * @author 小苏
     * @date 2023/02/01 18:10:39
     */
    @Override
    public List<Tblgame> findGameByType(int paramameterid) {
        return gameHallMapper.findGameByType(paramameterid);
    }

    /**
     * sys ins游戏房间
     *
     * @param gameid 游戏id
     * @return boolean
     * @author 小苏
     * @date 2023/02/02 15:47:11
     */
    @Override
    public boolean insGameRoomBySys(int gameid) {
        /**
         * 1.先判断是否创建了默认的房间数
         * 2.否的话  查出该游戏需要几个人 然后创建房间
         **/
        int countRommByGameid = gameHallMapper.countRommByGameid(gameid);
        if(countRommByGameid == 0){
            int gameNumById = gameHallMapper.findGameNumById(gameid);
            //创建房间
            for (int i = 0; i < 30; i++) {
                gameHallMapper.insGameRoomBySys(gameid, gameNumById);
            }
            return true;

        }
        return false;
    }

    /**
     * 发现房间listby gameid
     *
     * @param gameid gameid
     * @return {@link List }<{@link Tblgame }>
     * @author 小苏
     * @date 2023/02/02 18:38:09
     */
    @Override
    public List<MyGameRoom> findRoomListbyGameid(int gameid) {
        return gameHallMapper.findRoomListbyGameid(gameid);
    }

    /**
     * ins现在用户房间
     *
     * @param rid 房间id
     * @param uid 用户id
     * @return int
     * @author 小苏
     * @date 2023/02/07 15:46:00
     */
    @Override
    public int insNowUserToRoom(int rid, int uid) {
        int countRoomUser = gameHallMapper.countRoomUser(rid);
        if(countRoomUser == 0){
            //该房间没有玩家 将这个玩家当做房主
            int insNowUserToRoom = gameHallMapper.insNowUserToRoom(uid, rid, 0);
        }else if(countRoomUser == 1){
            int insNowUserToRoom = gameHallMapper.insNowUserToRoom(uid, rid, 1);
        }
        return 0;
    }


}
